package com.test.exoplayertest.player.manager;

import android.content.Context;
import android.graphics.Color;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.google.android.exoplayer2.SimpleExoPlayer;
import com.test.exoplayertest.player.gesture.GestureCallback;
import com.test.exoplayertest.player.gesture.GestureDetector;
import com.test.exoplayertest.player.interfaces.PlayerEventInterface;
import com.test.exoplayertest.player.view.PlayerFloatView;
import com.test.exoplayertest.player.view.PlayerPanelView;

import java.lang.ref.WeakReference;

/**
 * @author wuxiaotao on 2017/6/15.
 */

public class ViewControl implements PlayerEventInterface<ViewGroup> {

    private WeakReference<Context> mContextWeakReference;

    private TextureView mTextureView; //播放器显示view 最底层

    private ViewGroup mVideoRootView; //父布局

    private PlayerPanelView mPanelView; //控制面板

    private PlayerFloatView mFloatView; //loading  error view 进度 音量 亮度

    private View mShutterView; //surface 或者 textureview 黑色蒙层  防止透明透过

    private View mGestureView; //手势监听View

    private FrameLayout overlayFrame; //可供扩展的父布局

    private SimpleExoPlayer mExoPlayer;

    private GestureDetector detector; //手势管理

    public ViewControl(Context context) {
        mContextWeakReference = new WeakReference<Context>(context);
        initView(context);
    }

    @Override
    public void attach(ViewGroup viewGroup) {
        mVideoRootView = viewGroup;
        if (mTextureView.getParent() != null) {
            detach();
        }
        mPanelView.setExoPlayer(mExoPlayer);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addViewInPlayerLayout(mTextureView, params);
        addViewInPlayerLayout(mShutterView, params);
        addViewInPlayerLayout(mGestureView, params);
        addViewInPlayerLayout(mPanelView, params);
        addViewInPlayerLayout(mFloatView, params);
        addViewInPlayerLayout(overlayFrame, params);
        resume();
    }

    public void showLoadingView() {
        if (null != mFloatView) {
            mFloatView.showLoadingView();
        }
    }

    public void onVolumeChanged(int value, int max) {
        if (null != mFloatView) {
            mFloatView.showVolumeView(value, max);
        }
    }

    public void onBrightnessChanged(int value, int max) {
        if (null != mFloatView) {
            mFloatView.showBrightness(value, max);
        }
    }

    public void onSeekProgressChanged(long progress, long max) {
        if (null != mFloatView) {
            mFloatView.showSeekProgress(progress, max);
        }
    }

    public void showErrorView(String msg, View.OnClickListener listener) {
        if (null != mFloatView) {
            mFloatView.showErrorView(msg, listener);
        }
    }

    public void hideShutterView() {
        mShutterView.setVisibility(View.INVISIBLE);
    }

    private void showShutterView() {
        mShutterView.setVisibility(View.VISIBLE);
    }


    public void hideFloatView() {
        if (null != mFloatView) {
            mFloatView.setVisibility(View.GONE);
        }
    }

    @Override
    public void detach() {
        mVideoRootView.removeAllViews();
    }

    @Override
    public void startPlay(String url, String type) {
        mTextureView.setKeepScreenOn(true);
        showShutterView();
        showLoadingView();
    }

    @Override
    public void release() {

    }

    @Override
    public void resume() {
        showShutterView();
        showLoadingView();
    }

    public void setExoPlayer(SimpleExoPlayer exoPlayer) {
        mExoPlayer = exoPlayer;
        if (null != mExoPlayer && mPanelView != null) {
            mPanelView.release();
            mPanelView.setExoPlayer(mExoPlayer);
        }
    }

    public void setGestureListenner(GestureCallback callback) {
        if (null != detector) {
            detector.setGestureCallBack(callback);
        }

    }

    public TextureView getTextureView() {
        return mTextureView;
    }

    private void initView(Context context) {
        mTextureView = new TextureView(context);
        mShutterView = new View(context);
        mShutterView.setBackgroundColor(Color.BLACK);
        mPanelView = new PlayerPanelView(mContextWeakReference.get());
        mGestureView = new FrameLayout(context);
        mFloatView = new PlayerFloatView(context);
        overlayFrame = new FrameLayout(context);
        detector = new GestureDetector(mGestureView);
    }


    public void addViewInPlayerLayout(View childView, ViewGroup.LayoutParams params) {
        if (mVideoRootView != null && childView.getParent() == null) {
            mVideoRootView.addView(childView, params);
        }
    }

    public void addViewOverlayView(View view) {
        if (overlayFrame != null && view.getParent() == null) {
            overlayFrame.addView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        }
    }

    public void onPanelViewClick() {
        if (null != mPanelView) {
            if (mPanelView.isVisible()) {
                mPanelView.hide();
            } else {
                mPanelView.show();
            }
        }
    }

    public void showPanelView() {
        if (null != mPanelView) {
            mPanelView.show();
        }
    }

    /**
     * 所有的子view可以共用一个listenner  交给manager统一处理
     */
    public void setChildClickListenner(View.OnClickListener listenner) {
        if (null != mPanelView) {
            mPanelView.setChildClickListenner(listenner);
        }
    }

    public void onConfigurationChanged(boolean isLand){
        if(mPanelView != null){
            mPanelView.onConfigurationChanged(isLand);
        }
    }

}
